import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import 'button_widget.dart';

class DateSelectWidget extends StatefulWidget {
  final DateTime? startDateTime;
  final DateTime? endDateTime;
  final Function(DateTime)? startDateCallBack;
  final Function(DateTime)? endDateCallBack;

  const DateSelectWidget({Key? key, this.startDateTime, this.endDateTime, this.startDateCallBack, this.endDateCallBack})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DateSelectWidgetState();
  }
}

class _DateSelectWidgetState extends State<DateSelectWidget> {
  late DateTime startDateTime;
  late DateTime endDateTime;

  late DateTime maxDateTime;

  String startTime = "start_time".tr;
  String endTime = "end_time".tr;

  @override
  initState(){
    super.initState();
    startDateTime = widget.startDateTime ?? DateTime.now();
    endDateTime = widget.endDateTime ?? DateTime.now();
    var now = DateTime.now();
    maxDateTime = DateTime(now.year+1);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      width: double.infinity,
      color: Colors.white,
      height: 50.w,
      alignment: Alignment.centerLeft,
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: Text(
              "query_start_time".tr,
              style: TextStyle(fontSize: 13.sp),
            ),
          ),
          Expanded(
            flex: 1,
            child: ButtonWidget(
              onPressed: ()=> _selectDatePickerViewAction(1),
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.black26,
                  borderRadius: BorderRadius.all(Radius.circular(25.w)),
                ),
                width: double.infinity,
                height: 30.sp,
                child: Row(
                  children: [
                    const SizedBox(
                      width: 10,
                    ),
                    Text(
                      startTime,
                      style: TextStyle(fontSize: 13.sp),
                    ),
                    Expanded(
                      child: Icon(
                        Icons.calendar_month_sharp,
                        size: 20.sp,
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Text(
              "to".tr,
              style: TextStyle(fontSize: 13.sp),
            ),
          ),
          Expanded(
            flex: 1,
            child: ButtonWidget(
                onPressed: ()=> _selectDatePickerViewAction(2),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.black26,
                    borderRadius: BorderRadius.all(Radius.circular(25.w)),
                  ),
                  width: double.infinity,
                  height: 30.sp,
                  child: Row(
                    children: [
                      const SizedBox(
                        width: 10,
                      ),
                      Text(
                        endTime,
                        style: TextStyle(fontSize: 13.sp),
                      ),
                      Expanded(
                        child: Icon(
                          Icons.calendar_month_sharp,
                          size: 20.sp,
                        ),
                      ),
                    ],
                  ),
                )),
          )
        ],
      ),
    );
  }

  //type=1开始，type=2结束
  _selectDatePickerViewAction(int type) {
    _showDialog(
      CupertinoDatePicker(
        mode: CupertinoDatePickerMode.date,
        maximumDate: maxDateTime,
        onDateTimeChanged: (DateTime dateTime) {
          if (type == 1) {
            startDateTime = dateTime;
          }else{
            endDateTime = dateTime;
          }
        },
      ),type
    );
  }

  // This shows a CupertinoModalPopup with a reasonable fixed height which hosts CupertinoDatePicker.
  void _showDialog(Widget child,int type) {
    // showBottomSheet(context: context, builder: (BuildContext context) => dateView(child));
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => Container(
        // alignment: Alignment.bottomCenter,
        height: 256,
        padding: const EdgeInsets.only(top: 6.0),
        color: CupertinoColors.systemBackground.resolveFrom(context),
        margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        child: Column(
          children: [
            Container(
              width: double.infinity,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CupertinoButton(
                    padding: EdgeInsets.only(left: 20.sp, right: 20.sp),
                    minSize: 40,
                    onPressed: () {
                      Get.back();
                    },
                    child: Text(
                      "title_cancel".tr,
                      style: TextStyle(fontSize: 14.sp),
                    ),
                  ),
                  CupertinoButton(
                    padding: EdgeInsets.only(left: 20.sp, right: 20.sp),
                    minSize: 40,
                    onPressed: () {
                      Get.back();
                      clickConfirmAction(type);
                    },
                    child: Text(
                      "title_ok".tr,
                      style: TextStyle(fontSize: 14.sp),
                    ),
                  )
                ],
              ),
            ),
            Expanded(
              child: SafeArea(
                top: false,
                child: child,
              ),
            ),
          ],
        ),
      ),
    );
  }

  clickConfirmAction(int type) {
     DateFormat dateFormat = DateFormat("yyyy-mm-dd");
    if (type == 1) {
      startTime = dateFormat.format(startDateTime);
      setState((){});
      if (null != widget.endDateCallBack) {
        widget.endDateCallBack!(startDateTime);
      }
    }else {
      endTime = dateFormat.format(endDateTime);
      setState((){});
      if (null != widget.endDateCallBack) {
        widget.endDateCallBack!(startDateTime);
      }
    }
  }
}
